//
//  LeftViewController.m
//  SinceLe
//
//  Created by 李祎 on 15/11/12.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import "LeftViewController.h"
#import <RESideMenu.h>
#import "MCMutipleSectionTC.h"
#import "CDUserManager.h"
#import "MovieViewController.h"
#import "CDCommon.h"
#import "CDBaseTabC.h"
#import "MusicViewController.h"
#import "CDConvsVC.h"
#import "CDFriendListVC.h"
#import "CDProfileVC.h"
#import "CDBaseNavC.h"
#import "WeatherQueryViewController.h"
#import "NewsListViewController.h"
#import "FlipViewController.h"

#define WEAKSELF  typeof(self) __weak weakSelf = self;

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView* tableView;

@property (nonatomic, strong) UIImageView* headImageView;

@end

@implementation LeftViewController

-(void)loadTableView{
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width * 0.75, self.view.frame.size.height - 50) style:UITableViewStylePlain];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.opaque = NO;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView = nil;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.bounces = NO;
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        tableView.backgroundColor = [UIColor clearColor];
        tableView;
    });
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"返回主页";
            cell.imageView.image = [UIImage imageNamed:@"left_home"];
            break;
        case 1:
            cell.textLabel.text = @"今日头条";
            cell.imageView.image = [UIImage imageNamed:@"left_today"];
            break;
        case 2:
            cell.textLabel.text = @"天气查询";
            cell.imageView.image = [UIImage imageNamed:@"left_weather"];
            break;
        case 3:
            cell.textLabel.text = @"地图导航";
            cell.imageView.image = [UIImage imageNamed:@"left_map"];
            break;
        case 4:
            cell.textLabel.text = @"备忘录";
            cell.imageView.image = [UIImage imageNamed:@"left_note"];
            break;
            
        default:
            break;
    }
    return cell;
}

#pragma mark ---- 点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    FlipViewController *flip = [[FlipViewController alloc]initWithNibName:@"FlipViewController" bundle:nil];
    CDBaseTabC *tab = [[CDBaseTabC alloc] init];
    WEAKSELF;
    [weakSelf addItemController:[[MovieViewController alloc] init] toTabBarController:tab];
    [weakSelf addItemController:[[MusicViewController alloc] init] toTabBarController:tab];
    [weakSelf addItemController:[[CDConvsVC alloc] init] toTabBarController:tab];
    [weakSelf addItemController:[[CDFriendListVC alloc] init] toTabBarController:tab];
    [weakSelf addItemController:[[CDProfileVC alloc] init] toTabBarController:tab];
    
    switch (indexPath.row) {
        case 0:
            [self.sideMenuViewController setContentViewController:tab animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 1:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[NewsListViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            break;
        case 2:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:[[WeatherQueryViewController alloc] init]]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            
            break;
        case 4:
            [self.sideMenuViewController setContentViewController:[[UINavigationController alloc] initWithRootViewController:flip]
                                                         animated:YES];
            [self.sideMenuViewController hideMenuViewController];
            
            break;
            
        default:
            break;
    }
}

- (void)addItemController:(UIViewController *)itemController toTabBarController:(CDBaseTabC *)tab {
    CDBaseNavC *nav = [[CDBaseNavC alloc] initWithRootViewController:itemController];
    [tab addChildViewController:nav];
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

//设置头像
-(void)setHeadView{
    //创建一个view
    UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 150)];
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(headView.frame.size.width * 0.5 - 50, 25, 100, 100)];
    _headImageView.backgroundColor = [UIColor yellowColor];
    _headImageView.layer.masksToBounds = YES;
    _headImageView.layer.cornerRadius = 50;
    [headView addSubview:_headImageView];
    self.tableView.tableHeaderView = headView;
    
    //得到头像
    [[CDUserManager manager] getBigAvatarImageOfUser:[AVUser currentUser] block:^(UIImage *image) {
        _headImageView.image = image;
    }];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setHeadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self loadTableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
